package com.kfdx.dc.common.utils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.kfdx.dc.common.utils.file.FileUploadUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class QrCodeUtils {

    public static String makeQrCode(Long deprId,Long tableId) {
        String text = "https://www.example.com?deptId="+deprId+"&tableId="+tableId; // 你的H5页面URL
        int width = 300; // 二维码宽度  
        int height = 300; // 二维码高度  
        String filePath = FileUploadUtils.getDefaultBaseDir()+deprId+"-"+tableId+ "-qrcode.png"; // 生成的二维码图片路径

        try {
            // 设置二维码的参数  
            Map<EncodeHintType, Object> hints = new HashMap<>();
            hints.put(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.toString());

            // 生成二维码的BitMatrix  
            BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);

            // 将BitMatrix转换为图片并保存到文件  
            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", java.nio.file.Paths.get(filePath));

            System.out.println("二维码图片已生成：" + filePath);
        } catch (WriterException | IOException e) {
            e.printStackTrace();
            return "";
        }
        return filePath;
    }
}